package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/submit")
    public R orders(@RequestBody Orders orders) {
        ordersService.saveWithDetail(orders);
        return R.success("下单成功");
    }

    //TODO OrderDto
    @GetMapping("/userPage")
    public R getByPage(int page, int pageSize) {
        Page p = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(Orders::getCheckoutTime);
        Page res = ordersService.page(p, wrapper);
        return R.success(res);
    }

    //TODO
    @GetMapping("/list")
    public R getOrders() {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(Orders::getCheckoutTime);
        List<Orders> list = ordersService.list(wrapper);
        List<OrdersDto> detailList= list.stream().map(item -> {
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId, id);
            List<OrderDetail> list1 = orderDetailService.list(wrapper1);
            OrdersDto ordersDto = new OrdersDto();
            ordersDto.setOrderDetails(list1);
            return ordersDto;
        }).collect(Collectors.toList());
        return R.success(detailList);
    }

    @PostMapping("/again")
    public R orderAgain(@RequestBody Orders orders) {
        Long id = orders.getId();
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderId, id);
        List<OrderDetail> list = orderDetailService.list(wrapper);
        List<ShoppingCart> res =list.stream().map(item->{
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(item.getName());
            shoppingCart.setImage(item.getImage());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            if(item.getDishId()!=null){
                shoppingCart.setDishId(item.getDishId());
            }
            if(item.getSetmealId()!=null){
                shoppingCart.setSetmealId(item.getSetmealId());
            }
            shoppingCart.setDishFlavor(item.getDishFlavor());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setAmount(item.getAmount());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(res);
        return R.success(res);
    }

    @GetMapping("/page")
    public R getByPages(int page, int pageSize, Long number, String beginTime, String endTime) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(number != null, Orders::getId, number);
        wrapper.between(beginTime != null && endTime != null, Orders::getCheckoutTime, beginTime, endTime);
        wrapper.orderByDesc(Orders::getCheckoutTime);
        Page page1 = ordersService.page(pageInfo, wrapper);
        return R.success(page1);
    }

    @PutMapping
    public R modStatus(@RequestBody Orders orders) {
        String msg = null;
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getId, orders.getId());
        Orders one = ordersService.getOne(wrapper);
        Integer status = orders.getStatus();
        if (one.getStatus() == 2) {
            one.setStatus(status);
            msg = "订单已派送";
        } else if (one.getStatus() == 3) {
            one.setStatus(status);
            msg = "订单已完成";

        }
        ordersService.updateById(one);
        return R.success(msg);
    }

    @GetMapping("export")
    public void importToExcel(HttpServletResponse response) throws IOException {
        ordersService.excelExport(response);
    }
}
